package com.ywjt.pinkelephant.dynamic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.ywjt.pinkelephant.R;

import java.util.ArrayList;


public class DynamicHomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    // private final int mCountLimit = 3;
    private ArrayList<String> data;
    private LayoutInflater minflater;
    private Context mContext;
    private OnItemClickListener listener;

    private static final int ITEM_ONE = 1;
    private static final int ITEM_TWO = 2;
    private static final int ITEM_THREE = 3;

    public DynamicHomeAdapter(ArrayList<String> data, Context mContext) {
        this.data = data;
        this.minflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    private OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);

        void onSelected(View view, int position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder;
        if (viewType == ITEM_ONE) {
            view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_dynamic1, parent, false);//添加
            holder = new AnoViewHolder(view);
        } else if (viewType == ITEM_TWO) {
            view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_dynamic2, parent, false);
            holder = new AnoViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_dynamic3, parent, false);
            holder = new AnoViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof AnoViewHolder) {

        } else {
//            Glide.with(mContext).load(data.get(position)).
//                    placeholder(R.mipmap.default_image)
//                    .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)))
//                    .into(((ViewHolder) holder).imageView);
//            ((ViewHolder) holder).ivClear.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (clickListener != null) {
//                        clickListener.onSelected(view, position);
//                    }
//                }
//            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() ;
    }

    public class AnoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView img;


        public AnoViewHolder(@NonNull View itemView) {
            super(itemView);
            //  img = itemView.findViewById(R.id.ivBgImage);
          //  img.setOnClickListener((View.OnClickListener) this);
            itemView.setOnClickListener(this::onClick);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onClick(itemView, getAdapterPosition());
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_ONE;
        } else if (position == 2) {
            return ITEM_THREE;
        } else {
            return ITEM_TWO;
        }
    }
}